﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net;
using System.IO;
namespace pkmb_client_bin_uploadfiles
{
    public partial class MainPage : UserControl
    {
        OpenFileDialog opnf;
        WebClient client = new WebClient();
       
        public UriBuilder Uri
        {
            get;
            set;
        }
        public MainPage()
        {
            InitializeComponent();
        }
        
        private void btnupload_Click(object sender, RoutedEventArgs e)
        {
             opnf = new OpenFileDialog();
            opnf.Multiselect = true;
            var useraccepts = opnf.ShowDialog();
            if (useraccepts==true)
            {
                client.OpenWriteCompleted += new System.Net.OpenWriteCompletedEventHandler(client_OpenWriteCompleted);
                client.UploadProgressChanged += new System.Net.UploadProgressChangedEventHandler(client_UploadProgressChanged);
                //client.Headers["Content-Type"] = "multipart/form-data";
               
                foreach (var file in opnf.Files)
                {
                     this.Uri =new UriBuilder(this.Uri.ToString()+ string.Format("?filename={0}&length={1}",file.Name,(int)file.Length));
                  
                    //client.Headers["Content-Disposition"] = string.Format("form-data; name=\"file\"; filename={0}", file.Name);
                    client.OpenWriteAsync(Uri.Uri, "POST",file);
                }
            }
           
        }

        void client_UploadProgressChanged(object sender, System.Net.UploadProgressChangedEventArgs e)
        {
            progressbar1.Value = e.ProgressPercentage;
            lblbytesuploaded.Content = e.ProgressPercentage;
            lblbytetosupload.Content = e.TotalBytesToReceive;
        }

        void client_OpenWriteCompleted(object sender, System.Net.OpenWriteCompletedEventArgs e)
        {
            try
            {
                //foreach (var file in opnf.Files)
                //{
                var file = (FileInfo)e.UserState;
                    var inputstream = file.OpenRead();
                    var outputstream = e.Result;
                    int totalbytesread;
                    byte[] bytes = new byte[4096];
                    int start = 0;
                    int rate = 4096;
                    int end = (int)inputstream.Length;
                    do
                    {
                        if (end - start < 4096)
                            
                            rate= end - start;
                        totalbytesread = inputstream.Read(bytes, 0,rate);
                        outputstream.Write(bytes, 0, totalbytesread);
                        start += totalbytesread;
                    }
                    while (start < end);
                    inputstream.Close();
                    outputstream.Close();
                //}
            }
            catch (Exception exp)
            {
                MessageBox.Show("upload fail:"+e.Error.Message);
                return;
            }
            MessageBox.Show("upload complete!");
        }
    }
}
